/**
 * @file contests/cf1705/d/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/17 11:53:10
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  vector<ll> pos_s, pos_t;
  if (s[0] != t[0] || s[n - 1] != t[n - 1]) {
    cout << -1 << endl;
    return;
  }
  for (int i = 0; i < n - 1; ++i) {
    if (s[i] != s[i + 1]) pos_s.push_back(i);
    if (t[i] != t[i + 1]) pos_t.push_back(i);
  }
  if (pos_s.size() != pos_t.size()) {
    cout << -1 << endl;
    return;
  } else {
    int k = pos_s.size();
    ll ans = 0;
    for (int i = 0; i < k; ++i) {
      ans += abs(pos_s[i] - pos_t[i]);
    }
    cout << ans << endl;
  }
}
int main() {
  // High rating and good luck!
  int tc;
  cin >> tc;
  while (tc--) solve();
  return 0;
}
